/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IRightInfoDAO;
import com.ssyt.examinationsystem.entity.RightInfo;
import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class RightInfoDAOImpl implements IRightInfoDAO {

    /*
     * 通过角色编号获取所有主菜单集合
     *
     * @see
     * com.ssyt.examinationsystem.dao.IRightInfoDAO#getMainMenuByRoleId(com.ssyt
     * .examinationsystem.entity.RoleInfo)
     */
    @Override
    public List<Map<String, Object>> getMainMenuByRoleId(RoleInfo role) {
        String sql = "select * from rightInfo where rightId in (select rightId from relationinfo where roleId = ?) and parentId='root'";
        return JdbcUtils.executeQuery(sql, role.getRoleId());
    }

    /*
     * 通过角色编号获取所有子菜单集合
     *
     * @see
     * com.ssyt.examinationsystem.dao.IRightInfoDAO#getChildMenuByRoleId(com.
     * ssyt.examinationsystem.entity.RoleInfo)
     */
    @Override
    public List<Map<String, Object>> getChildMenuByRoleId(RoleInfo role) {
        String sql = "select * from rightInfo where rightId in (select rightId from relationinfo where roleId = ?) and parentId<>'root'";
        return JdbcUtils.executeQuery(sql, role.getRoleId());
    }

    /**
     * 获取所有主菜单集合
     */
    public List<Map<String, Object>> getMainMenus() {
        String sql = "select * from rightInfo where parentId='root'";
        return JdbcUtils.executeQuery(sql);
    }

    /**
     * 获取所有子菜单集合
     */
    public List<Map<String, Object>> getChildMenus() {
        String sql = "select * from rightInfo where parentId<>'root'";
        return JdbcUtils.executeQuery(sql);
    }

    /**
     * 根据角色编号判断是否有权限
     *
     * @param roleId
     * @return
     */
    public boolean isGrantByRoleId(String roleId) {
        String sql = "select 1 from relationInfo where roleId=?";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, roleId);
        return CollectionUtils.isEmptyForList(list);
    }

    /**
     * 判断是否删除了权限
     *
     * @param roleId
     * @return
     */
    public boolean deleteGrantByRoleId(String roleId) {
        String sql = "delete from relationInfo where roleId=?";
        return JdbcUtils.executeUpdate(sql, roleId) > 0;
    }

    /**
     * 添加权限
     *
     * @param roleId
     * @param ids
     * @return
     */
    public int addGrantByRoleId(String roleId, String rightId) {
        String sql = "insert into relationInfo values(null,?,?)";
        return JdbcUtils.executeUpdate(sql, roleId, rightId);
    }

    /**
     * 获取根菜单和主菜单
     */
    public List<Map<String, Object>> getSysMainMenus() {
        String sql = "select * from rightInfo where parentId='root' or rightId='root'";
        return JdbcUtils.executeQuery(sql);
    }

    /**
     * 添加权限的实现方法
     */
    public int addRightInfo(RightInfo right) {
        String sql = "insert into rightInfo values(?,?,?,?)";
        Object[] parameters = {right.getRightId(), right.getParentId(), right.getRightName(), right.getRightUrl()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    /**
     * 根据父权限编号查询权限编号
     */
    public String getMaxRightIdByParentId(String parentId) {
        String sql = "select rightId from rightInfo where parentId=? order by rightId desc limit 1 ";
        List<Map<String, Object>> list = JdbcUtils.executeQuery(sql, parentId);
        return CollectionUtils.isEmptyForList(list) ? String.valueOf(list.get(0).get("rightId")) : null;
    }

}
